--- 模块功能：MQTT客户端数据接收处理
-- @author openLuat
-- @module mqtt.mqttInMsg
-- @license MIT
-- @copyright openLuat
-- @release 2018.03.28

module(...,package.seeall)
require "mqttTask"
require "mqttOutMsg"
local setGpio19Fnc = pins.setup(pio.P0_19,1)
--[[sys.taskInit(function ()
    while true do
        log.info("tag123")
        setGpio19Fnc(0)
        sys.wait(1000)
        setGpio19Fnc(1)
        sys.wait(1000)
    end
end)]]
--- MQTT客户端数据接收处理
-- @param mqttClient，MQTT客户端对象
-- @return 处理成功返回true，处理出错返回false
-- @usage mqttInMsg.proc(mqttClient)
function proc(mqttClient,topic1,topic2)
    local result,data
    while true do
        result,data = mqttClient:receive(60000,"APP_SOCKET_SEND_DATA")
        --接收到数据
        if result then
            log.info("mqttInMsg.proc",data.topic,data.payload)
            local tjsondata,result,errinfo=json.decode(data.payload)--------解析json数据--------   
            local IMEI=mqttTask.get_IMEI()
            --TODO：根据需求自行处理data.payload
            if data.topic==topic1 then
                if result and type(tjsondata)=="table" then   
                    log.info("222222222222222222222222222")
                    local ss=tonumber(tjsondata["relayStatus"])
                   
                    local ds=string.format( "{\"deviceNum\":\"%s\",\"relayStatus\":%d,\"isOnline\":1}",IMEI,ss)
                    setGpio19Fnc(ss)
                    mqttTask.set_Status(ss)
                    mqttOutMsg.insertMsg("status",ds,0)
                    log.info("12345677",tostring(ss))
                end
            end
            if data.topic==topic2 then
                local status=mqttTask.get_Status()
                local ds =string.format( "{\"deviceNum\":\"%s\",\"relayStatus\":%d,\"isOnline\":1}",IMEI,status)
                mqttOutMsg.insertMsg("status",ds,0)
            end
            
        else
            break
        end
    end
	
    return result or data=="timeout" or data=="APP_SOCKET_SEND_DATA"
end
